import React, { memo, useEffect, useState } from "react";
import { Button, Checkbox, Form, Input, Card, message } from "antd";
import { LoginWrapper } from "./style.js";
import { login } from "@/service/user";
// import bg from "./bg";
const Login = memo((props) => {
  const [username] = useState("admin");
  const [password] = useState("123456");
  const [isLoading, setIsLoading] = useState(false);
  useEffect(() => {
    // bg();
  }, []);
  const handleLogin = (values) => {
    // console.log("Success:", values);
    setIsLoading(true);
    login({
      username: values.username.trim(),
      password: values.password.trim(),
    })
      .then((res) => {
        console.log(res);
        setIsLoading(false);
        message.success("登录成功,即将跳转到首页!");
        // 跳转到首页
        // console.log(props);
        props.history.push("/");
      })
      .catch((err) => {
        setIsLoading(false);
      });
  };
  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };
  return (
    <LoginWrapper>
      <canvas id="login-bg"></canvas>
      <Card className="login-form" title="请登录账号">
        <Form
          name="basic"
          labelCol={{
            span: 8,
          }}
          wrapperCol={{
            span: 16,
          }}
          initialValues={{
            remember: true,
          }}
          onFinish={handleLogin}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="Username"
            name="username"
            initialValue={username}
            rules={[
              {
                required: true,
                message: "Please input your username!",
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="Password"
            name="password"
            initialValue={password}
            rules={[
              {
                required: true,
                message: "Please input your password!",
              },
            ]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item
            name="remember"
            valuePropName="checked"
            wrapperCol={{
              offset: 8,
              span: 16,
            }}
          >
            <Checkbox>Remember me</Checkbox>
          </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 8,
              span: 16,
            }}
          >
            <Button type="primary" htmlType="submit" loading={isLoading}>
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </LoginWrapper>
  );
});

export default Login;
